/**
  ******************************************************************************
  * @file    main.c
  * @author  huhao
  * @version V1.000
  * @date    09-15-2020
  * @project linux client
  ******************************************************************************
  * @attention 因为使用环境的问题，该程序只能通过参数启动，不应通过配置文件启动，可使用
  *启动脚本来进行管理,代码中出现的e.g &sodf12sadfos@#,定义为注释指纹，方便用来定位对应
  *的注释
  *
  * @brief   主程序首先解析配置文件，然后根据命令行参数决定程序执行流程
  *          -help:
  *             打印程序帮助信息;
  *          -a [ip_address]
  *             ip address;
  *          -p [port_num]
  *             port;
  *
  *          -t [rsa|sm]
  *             type of cert;
  *          -ca [cert_path]
  *             cert info;
  * <h2><center>&copy; COPYRIGHT 2020 上海今中网络科技有限公司 </center></h2>
  ******************************************************************************
  */

#include "main_bean.h"


/**
  * @brief  解析命令行参数
  * @param  arg1:arc  arg2:argv  arg3:commandParam  arg4:signalUser
  * @retval void
  */
static void
parse_server_command(int argc, char *argv[], COMMAND_PARAM_T *commandParam, int a)
{
    struct option serverOptions[] =
               {{"start",      no_argument,       NULL, OPT_START      },
                {"stop",       no_argument,       NULL, OPT_STOP       },
                {"configpath", required_argument, NULL, OPT_CONFIG_PATH},
                {"ip",         required_argument, NULL, OPT_ADDRESS    },
                {"ca",         no_argument,       NULL, OPT_RELOAD_CA  },
                {"crl",        no_argument,       NULL, OPT_RELOAD_CRL },
                {"version",    no_argument,       NULL, OPT_VERSION    },
                {"help",       no_argument,       NULL, OPT_HELP       },
                {NULL,         0,                 NULL, 0              }};

    ZeroMemory(commandParam, sizeof(COMMAND_PARAM_T));

    while (TRUE)
    {
        switch(getopt_long_only(argc, argv, "asSvh", serverOptions, NULL))
        {
            case 's' :
            case OPT_START :
                commandParam->start = 1;
                break;

            case 'S' :
            case OPT_STOP :
                commandParam->stop = 1;
                break;

            case OPT_CONFIG_PATH :
                //SERVER_CNF_PATH = optarg;
                //if (access(SERVER_CNF_PATH, 0) != 0)
                //    MSG(LOG_FATAL, "The config file specified by manager can not access!");
                break;

            case OPT_RELOAD_CA :
                commandParam->reloadCA = 1;
                break;

            case OPT_RELOAD_CRL :
                commandParam->reloadCRL = 1;
                break;

            case 'v' :
            case OPT_VERSION :
                commandParam->version = 1;
                break;

            case 'h' :
            case OPT_HELP :
                commandParam->help = 1;
                break;

            case 'a' :
            case OPT_ADDRESS :
                commandParam->address  = 1;
                commandParam->remmsg   = optarg;
                break;
            case -1:
            default :
                return;
        }
    }
}

int main(int argc, char ** argv)
{
    int retval = 0;
    COMMAND_PARAM_T commandParam;


    parse_server_command(argc, argv, &commandParam, 1);

        /// 获取服务器版本号
    if (commandParam.version)
    {
        show_version();
    }
    /// 显示帮助
    else if (commandParam.help)
    {
        show_help();
    }
    /// 关闭程序
    else if (commandParam.stop)
    {
        MSG(LOG_DEBUG, "[DEBUG] [stop master porcess]");
        stop_master_process();
    }
    /// 启动程序
    else if (commandParam.start)
    {
        MSG(LOG_DEBUG, "[DEBUG] [start master porcess] before demonize without parameter");
        start_master_process(argc, argv);    //use default config
    }
    /// 启动程序
    else if (commandParam.address)
    {
        if(NULL != commandParam.remmsg)
        {
            MSG(LOG_DEBUG, "[DEBUG][start master porcess] before demonize");
            //start_master_process(argc, commandParam.remmsg);
        }
        else
        {
            MSG(LOG_ERROR, "[ERROR] -a need the [ip] string");
        }
    }
    else
    {
        show_help();
    }

    return retval;
}
